﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace Abide.Forms
{
    public partial class News : Form
    {
        //Store our Release Information
        Classes.ReleaseInfo ReleaseInfo;
        bool Clickable = false;
        string newsHtml = string.Empty;

        public News()
        {
            InitializeComponent();
        }

        private void News_Load(object sender, EventArgs e)
        {
            //Asynchronously Load News
            System.Threading.ThreadPool.QueueUserWorkItem(LoadNews);
            newsBrowser.Navigate("https://abide.svn.codeplex.com/svn/news/index.html");
        }

        private void News_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Cancel?
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                WindowState = FormWindowState.Minimized;
            }
        }

        private void LoadComplete()
        {
            //Wait for browser to be ready...
            //while (newsBrowser.IsBusy)
            //{ System.Threading.Thread.Sleep(1); }

            //Set Document
            //newsBrowser.DocumentText = newsHtml;
            newsBrowser.Navigate("https://abide.svn.codeplex.com/svn/news/index.html");

            //Check...
            if (ReleaseInfo != null)
            {
                //Edit Labels
                titleLabel.Text = ReleaseInfo.Title;

                //Get Latest Release
                Version CurrentVersion = new Version(Application.ProductVersion);
                Classes.ReleaseInfo.Release Latest = null;
                foreach (var Release in ReleaseInfo.Releases)
                    if (Latest == null)
                        Latest = Release;
                    else
                        if (Release.Category == (Classes.ReleaseCategory)Enum.Parse(typeof(Classes.ReleaseCategory), Abide_AddOn_API.Classes.Settings.UpdateThreshold))
                            if (Release.Version > Latest.Version)
                                Latest = Release;
                            else if (Release.Version == Latest.Version)
                                Latest = Release;

                //Check if newer...
                if (CurrentVersion < Latest.Version)
                {
                    WindowState = FormWindowState.Normal;
                    releasePanel.BackColor = Color.SteelBlue;
                    BringToFront();
                }

                //Edit Labels
                versionLabel.Text = Latest.Version.ToString();
                urlLink.Text = Latest.Url; urlLink.Links.Add(0, Latest.Url.Length, Latest.Url);
                Clickable = true;
            }
            else
            {
                versionLabel.Text = "0.0.0.0";
                urlLink.Text = "download failed";
                titleLabel.Text = "Failed to retrieve release information";
            }

            //Edit Visibility
            loadLabel.Visible = false;
            infoPanel.Visible = true;
            newsBrowser.Visible = true;
        }

        private void LoadNews(object state)
        {
            //Download News
            try
            {
                WebClient Wc = new WebClient();
                newsHtml = Wc.DownloadString("https://abide.svn.codeplex.com/svn/news/index.html");
                Wc.Dispose();
            }
            catch (Exception ex)
            { newsHtml = "<!DOCTYPE HTML><html><body><h>{0}</h><p>{1}</p></body></html>"; newsHtml = string.Format(newsHtml, ex.GetType().ToString(), ex.Message); }

            //Check
            if (string.IsNullOrEmpty(newsHtml))
                newsHtml = "<!DOCTYPE HTML><html><body><h1>Unable to retrieve news</h1><p>Sorry about that!</p></body></html>";

            //Load News
            ReleaseInfo = Classes.ReleaseInfo.Load();

            //Invoke Panel Edits
            Invoke(new MethodInvoker(LoadComplete));
        }

        private void urlLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (Clickable)
                System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
        }
    }
}
